package fm.dongman.modellib.remote;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import fm.dongman.animefm.contractlib.contract.FRankContract;
import fm.dongman.animefm.contractlib.model.FRankDataModel;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;

/**
 * 榜单网络数据加载
 * Created by Administrator on 2017/10/20.
 */

public class FRankRemoteDataSource implements FRankContract.IFRankDataSource {
    private static FRankRemoteDataSource SINGLETON; // 实例
    private static final String MORE_RANK_URL = "http://m.dongman.fm/rank"; // 更多漫榜地址
    private RemoteOperationHelper mRemote;

    private boolean isRemote = true;

    public FRankRemoteDataSource() {
        mRemote = RemoteOperationHelper.getOperationHelper();
    }

    public static FRankRemoteDataSource getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new FRankRemoteDataSource();
        }
        return SINGLETON;
    }

    @NotNull
    @Override
    public String getMoreRank() {
        return MORE_RANK_URL;
    }

    @Override
    public void getData(@NotNull LoadSourceCallback<? super FRankDataModel> callback) {
        if (isRemote) {
            JSONObject json = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.FIND_RANK_REMOTE + "page=1&size=5");
            if (json == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(mRemote.downFrankData(json));
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(FRankDataModel dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
